package com.rois.librois.libRoISMessageStorage;

import java.util.Vector;

import com.rois.librois.libRoISEngineClient.ClientOperationImpl;

/**
 * Data holder class for RoIS Engine.
 * This object holds CORBA client object and a list of:
 * - connection status
 * - connecting CORBA server name
 * - CORBA naming service
 * - subscribed event messages
 * - requested command messages
 * - sent error messages
 * - number of bind
 *
 * @author Miki Sato <miki.sato.1225@gmail.com>
 *
 *    The MIT licence
 *
 *    Copyright (C) 2011
 *      ATR Intelligent Robotics and Communication Laboratories, Japan
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is furnished to do so,
 *    subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included in all
 *    copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class RoIS_Engines {
	private static final int DEFAULT_ENG_MAXSIZE = 100;
	private static final int DEFAULT_TYPE_MAXSIZE = 100;
	private static final int DEFAULT_MSG_MAXSIZE = 100;

	public int size;
	public int max_size;
	public int max_cmd_size;
	public int max_evt_size;
	public int max_cmd_message_size;
	public int max_evt_message_size;
	public int max_err_message_size;

	public ClientOperationImpl client_p;
	public Vector<Boolean> connection = new Vector<Boolean>();
	public Vector<String> nameservice = new Vector<String>();
	public Vector<String> servername = new Vector<String>();

	public Vector<RoIS_RegisteredTypes> subscribed_events = new Vector<RoIS_RegisteredTypes>();;
	public Vector<RoIS_RegisteredTypes> requested_commands = new Vector<RoIS_RegisteredTypes>();
	public RoIS_ExchangedMessages sent_errors = new RoIS_ExchangedMessages();
	int error_count;

	public Vector<Integer> bind_size = new Vector<Integer>();

	/**
	 * create RoIS Engine data holder.
	 */
	public RoIS_Engines()
	{
		client_p = null;
		max_size = DEFAULT_ENG_MAXSIZE;
		max_cmd_size = DEFAULT_TYPE_MAXSIZE;
		max_evt_size = DEFAULT_TYPE_MAXSIZE;
		max_cmd_message_size = DEFAULT_MSG_MAXSIZE;
		max_evt_message_size = DEFAULT_MSG_MAXSIZE;
		max_err_message_size = DEFAULT_MSG_MAXSIZE;
		clear();
	}

	/**
	 * clear all data.
	 */
	public void clear()
	{
		if(client_p!=null)
		{
			client_p = null;
		}
		client_p = new ClientOperationImpl();

		size = 0;
		connection.clear();
		bind_size.clear();
		nameservice.clear();
		servername.clear();

		subscribed_events.clear();
		requested_commands.clear();
		sent_errors.clear();
		sent_errors.max_size = max_err_message_size;
		error_count = 0;
	}

	/**
	 * add Engine at the end of engine list.
	 * @param _connection connection status
	 * @param _bind_size number of bind
	 * @param _nameservice CORBA naming service
	 * @param _servername CORBA server name
	 */
	public void push_back(
		boolean _connection,
		int _bind_size,
		String _nameservice,
		String _servername
	)
	{
		connection.add(_connection);
		nameservice.add(_nameservice);
		servername.add(_servername);

		RoIS_RegisteredTypes _subscribed_events = new RoIS_RegisteredTypes();
		_subscribed_events.max_size = max_evt_size;
		_subscribed_events.max_message_size = max_evt_message_size;
		subscribed_events.add(_subscribed_events);

		RoIS_RegisteredTypes _requested_commands = new RoIS_RegisteredTypes();
		_requested_commands.max_size = max_cmd_size;
		_requested_commands.max_message_size = max_cmd_message_size;
		requested_commands.add(_requested_commands);

		bind_size.add(_bind_size);

		size++;
		if(size>max_size)
		{
			erase(0);
		}
	}

	/**
	 * remove Engine at the end of engine list.
	 */
	public void pop_back()
	{
		connection.remove(connection.size()-1);
		nameservice.remove(nameservice.size()-1);
		servername.remove(servername.size()-1);

		subscribed_events.remove(subscribed_events.size()-1);
		requested_commands.remove(requested_commands.size()-1);

		bind_size.remove(bind_size.size()-1);
		size--;
	}

	/**
	 * remove specified engine from engine list.
	 * @param index index of the engine
	 */
	public void erase(
		int index
	)
	{
		connection.remove(index);
		nameservice.remove(index);
		servername.remove(index);

		subscribed_events.remove(index);
		requested_commands.remove(index);

		bind_size.remove(index);
		size--;
	}

	/**
	 * get index of a engine related to server name.
	 * @param _servername CORBA server name of the engine
	 * @return index of the engine
	 */
	public int getIndexByServername(
		String _servername
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			if(servername.get(i).matches(_servername))
			{
				iRes = i;
				break;
			}
		}
		return iRes;
	}

	/**
	 * create new error message ID.
	 * @return new error ID
	 */
	public String createErrorID()
	{
		error_count++;
		String strRes = String.valueOf(error_count);
		return strRes;
	}
}
